'''
第二步：特征工程
这次数据类型单一，都是数值型数据。在数据探索中我们发现了很多缺失值，我们在特征工程中填充或处理。
'''

import numpy as np 
import pandas as pd 
from sklearn.preprocessing import StandardScaler

train = pd.read_csv('pima-indians-diabetes.csv')
# 设置pandas显示所有行和列
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
#print(train.describe())
Nan_colomns = ['Plasma_glucose_concentration', 'blood_pressure', 'Triceps_skin_fold_thickness', 'serum_insulin', 'BMI']
print((train[Nan_colomns] == 0).sum())
# 将无效的0值替换为numpy的NaN
train[Nan_colomns] = train[Nan_colomns].replace(0, np.NaN)
print(train.isnull().sum())
medians = train.median()
train = train.fillna(medians)
print(train.describe())
# 数据标准化
ss_X = StandardScaler()
y_train = train['Target']
X_train = train.drop('Target', axis=1)
feature_names = X_train.columns
X_train = ss_X.fit_transform(X_train)
# 保存文件
X_train = pd.DataFrame(data=X_train, columns=feature_names)
train = pd.concat([X_train, y_train], axis=1)
train.to_csv('pima-indians-diabetes_FE.csv', index=False, header=True)
